﻿using System;
using System.Collections.Generic;
using System.Linq;
using DMS.Business.Utility;
using DMS.Business.Modules.Organization;

namespace DMS.Business.Modules.SupplierManagement
{
    /// <summary>
    /// 供应商用户(帐号)
    /// </summary>
    [Serializable]
    public class Supplier : Actor
    {
        #region 数据成员
        protected string _ShortName = string.Empty;
        protected string _Contact = string.Empty;
        protected string _Phone = string.Empty;
        protected string _Mobile = string.Empty;
        protected string _Fax = string.Empty;
        protected string _Email = string.Empty;
        protected string _Address = string.Empty;
        protected bool _IsDeleted = false;
        
        protected List<SupplierAccount> _Accounts = null;
        protected SupplierAccount _DefaultAccount = null;
        #endregion

        #region 成员属性
        /// <summary>
        /// 成员类型
        /// </summary>
        public override ActorTypes ActorType
        {
            get
            {
                return ActorTypes.Supplier;
            }
            set
            {
                
            }
        }

        public string SupplierID
        {
            get
            {
                return ID;
            }
        }
        public string SupplierName
        {
            get
            {
                return Name;
            }
        }

        public string ShortName
        {
            get { return _ShortName; }
            set { _ShortName = value; }
        }
        public string Contact
        {
            get { return _Contact; }
            set { _Contact = value; }
        }
        public string Phone
        {
            get { return _Phone; }
            set { _Phone = value; }
        }
        public string Mobile
        {
            get { return _Mobile; }
            set { _Mobile = value; }
        }
        /// <summary>
        /// 供应商简称
        /// </summary>
        public string Fax
        {
            get { return _Fax; }
            set { _Fax = value; }
        }
        public string Email
        {
            get { return _Email; }
            set { _Email = value; }
        }

        public string Address
        {
            get { return _Address; }
            set { _Address = value; }
        }
        public bool IsDeleted
        {
            get { return _IsDeleted; }
            set { _IsDeleted = value; }
        }


        public List<SupplierAccount> Accounts
        {
            get
            {
                if (this._Accounts == null)
                {
                    this._Accounts = SupplierHelper.GetAccountBySPID(this._ID);
                }
                return this._Accounts;
            }
        }
        public SupplierAccount DefaultAccount
        {
            get
            {
                if (this._DefaultAccount == null && this.Accounts != null && this.Accounts.Count > 0)
                {
                    this._DefaultAccount = (from a in this.Accounts
                                            where a.IsDefaultAccount == true
                                            select a).DefaultIfEmpty(null).FirstOrDefault();

                }
                return this._DefaultAccount;
            }
        }
        #endregion

        #region 构造函数
        public Supplier(string id)
            : base(id)
        {
        }

        public Supplier()
        {
        }
        #endregion

        #region 成员方法
        public void Save()
        {
            if (string.IsNullOrEmpty(_ID))
            {
                this._ID = Guid.NewGuid().ToString();

                SupplierHelper.AddSupplier(this);
            }
            else
            {
                SupplierHelper.UpdateSupplier(this);
            }
        }

        public void Delete()
        {
            SupplierHelper.DeleteSupplier(this);
        }
        #endregion
    }
}
